package cn.buk.qms.web;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.tms.dto.TrainRefundOrderDto;
import cn.buk.tms.entity.misc.OperationLog;
import cn.buk.tms.train.sc.TrainOrderSearchCriteria;
import cn.buk.tms.train.service.TrainRefundOrderService;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 火车票退票订单
 * @author yfdai
 * @date 2019-10-13
 */
@RestController
@RequestMapping("/train/refund")
public class TrainRefundOrderController extends BaseController {

    private static final Logger logger = LogManager.getLogger(TrainRefundOrderController.class);

    @Autowired
    private TrainRefundOrderService service;

    /**
     * 搜索火车票退票订单
     * @param request
     * @param sc
     * @return
     */
    @RequestMapping("/orders")
    public CommonDto<TrainRefundOrderDto> searchOrders(HttpServletRequest request,
                                                    @ModelAttribute("sc") TrainOrderSearchCriteria sc) {
        final int enterpriseId = getEnterpriseId(request);
        final int userId = getUserId(request);

        List<TrainRefundOrderDto> datas = service.searchOrders(enterpriseId, userId, sc);

        CommonDto<TrainRefundOrderDto> commonDto = new CommonDto<>();
        commonDto.setDataList(datas);
        commonDto.setPage(sc.getPage());

        return commonDto;
    }

    /**
     * 获取火车票退票订单详情
     * @param request
     * @return
     */
    @RequestMapping("/order/{orderId}")
    public TrainRefundOrderDto searchOrder(HttpServletRequest request,
                                        @PathVariable("orderId") final int orderId) {
        final int enterpriseId = getEnterpriseId(request);

        return service.getOrderById(enterpriseId, getUserId(request), orderId);
    }

    /**
     * 创建火车票退票订单
     * @param request
     * @param dto
     * @return
     */
    @RequestMapping(value = "/order", consumes = "application/json", method = RequestMethod.POST)
    public JsonResult createOrder(HttpServletRequest request,
                                       @RequestBody TrainRefundOrderDto dto) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.createOrder(enterpriseId, username, dto);
    }


    /**
     * 修改退票订单的备注
     * @param request
     * @param orderId
     * @param remark
     * @return
     */
    @RequestMapping(value = "/order/{orderId}/remark", method = RequestMethod.POST)
    public JsonResult updateRemark(HttpServletRequest request,
                                             @PathVariable("orderId") final int orderId,
                                             @RequestParam("remark") String remark) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.updateOrderRemark(enterpriseId, username, orderId, remark);
    }

    /**
     * 修改退票外部订单号
     * @param request
     * @param orderId
     * @param externalOrderNo
     * @return
     */
    @RequestMapping(value = "/order/{orderId}/externalOrderNo", method = RequestMethod.POST)
    public JsonResult updateExternalOrderNo(HttpServletRequest request,
                                             @PathVariable("orderId") final int orderId,
                                             @RequestParam("externalOrderNo") String externalOrderNo) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.updateOrderExternalOrderNo(enterpriseId, username, orderId, externalOrderNo);
    }


    /**
     * 修改订单的价格
     */
    @RequestMapping(value = "/order/{orderId}/price", method = RequestMethod.POST)
    public JsonResult updatePrice(HttpServletRequest request,
                                          @PathVariable("orderId") int orderId,
                                          @RequestParam("price") double price,
                                          @RequestParam("serviceCharge") double serviceCharge,
                                          @RequestParam("cost") double cost
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.updateOrderPrice(enterpriseId, username, orderId, price, cost, serviceCharge);
    }


    /**
     * 开始处理火车票订单
     * @param request
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/order/{orderId}/status/processing", method = RequestMethod.POST)
    public JsonResult processOrder(HttpServletRequest request,
                                                     @PathVariable("orderId") final int orderId) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.processOrder(enterpriseId, username, orderId);
    }

    /**
     * 火车票订单处理完毕
     * @param request
     * @param orderId
     * @param remark
     * @return
     */
    @RequestMapping(value = "/order/{orderId}/status/finished", method = RequestMethod.POST)
    public JsonResult finishOrder(HttpServletRequest request,
                                        @PathVariable("orderId") final int orderId,
                                       @RequestParam("remark") String remark) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.finishOrder(enterpriseId, username, orderId, remark);
    }

    @RequestMapping(value = "/order/{orderId}/status/canceled", method = RequestMethod.POST)
    public JsonResult cancelTrainOrder(HttpServletRequest request,
                                       @PathVariable("orderId") final int orderId,
                                       @RequestParam("remark") String remark) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.cancelOrder(enterpriseId, username, orderId, remark);
    }

    @RequestMapping(value = "/order/{orderId}/history")
    public List<OperationLog> searchTrainOrderHistory(HttpServletRequest request,
                                                      @PathVariable("orderId") final int orderId) {
        final int enterpriseId = getEnterpriseId(request);

        return service.searchOrderHistory(enterpriseId, orderId);
    }
}
